﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Forms.Properties;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace DevelopTool.Forms.DocumentForms
{
    /// <summary>
    /// Linux命令手册
    /// </summary>
    public partial class LinuxCommandForm : BaseUIPage
    {
        /// <summary>
        /// DataGridView列信息集合
        /// </summary>
        private List<DataGridColumn> dataGridColumns = new List<DataGridColumn>()
              {
                { new DataGridColumn("Command"){ColumnText="命令",Width=150,ColumnType=ColumnType.DefaultFilter}},
                { new DataGridColumn("Description"){ColumnText="描述",Width=0,ColumnType=ColumnType.DefaultFilter}},
                { new DataGridColumn("MarkDown"){ColumnText="文档内容",Width=-1,ColumnType=ColumnType.DefaultFilter}},//宽度给到最小值，显示了也没用，看不清
              };


        public LinuxCommandForm()
        {
            InitializeComponent();
        }


        private void LinuxCommandForm_Load(object sender,EventArgs e)
        {
            DataTable resultTable = new DataTable("ResultTable");
            resultTable.Columns.Add("Command",typeof(string));
            resultTable.Columns.Add("Description",typeof(string));
            resultTable.Columns.Add("MarkDown",typeof(string));

            if(Resources.LinuxCommand_Json != null && Resources.LinuxCommand_Json.Length > 0)
            {

                //解析json文件为字典集合
                Dictionary<string,LinuxCommand> linuxCommands = new Dictionary<string,LinuxCommand>();

                //加载markdown文档到字典集合
                Dictionary<string,byte[]> dic = new Dictionary<string,byte[]>();

                LoadingHelper.ShowLoading("正在准备文档",() =>
                {
                    linuxCommands = ZipHelper.GetContent(Resources.LinuxCommand_Json).DeserializeObject<Dictionary<string,LinuxCommand>>();

                    if(Resources.LinuxCommandMarkDown != null && Resources.LinuxCommandMarkDown.Length > 0)
                        dic = ZipHelper.GetDictionary(Resources.LinuxCommandMarkDown);//解压zip文件
                });


                foreach(var linuxCommand in linuxCommands)
                {
                    DataRow dataRow = resultTable.NewRow();

                    dataRow["Command"] = linuxCommand.Key;
                    dataRow["Description"] = linuxCommand.Value.d;

                    if(dic.TryGetValue($"{linuxCommand.Key}.md",out byte[] bytes))
                    {
                        dataRow["MarkDown"] = Encoding.UTF8.GetString(bytes);
                    }
                    else if(dic.TryGetValue($"{linuxCommand.Key}.markdown",out bytes))
                    {
                        dataRow["MarkDown"] = Encoding.UTF8.GetString(bytes);
                    }

                    resultTable.Rows.Add(dataRow);
                }
                dic.Clear();//清空掉吧，别一直占着内存
                dic = null;
            }

            this.dataDisplayControl.BindData(resultTable,this.dataGridColumns);

        }


        /// <summary>
        /// DataGridView单元格单击事件
        /// </summary>
        /// <param name="mouseButton"></param>
        /// <param name="column"></param>
        /// <param name="row"></param>
        /// <param name="cell"></param>
        private void dataDisplayControl_DataGridViewCellClick(DataGridViewEventArgs dataGridViewEventArgs)
        {
            string command = dataGridViewEventArgs.Row.Cells["Command"].Value.ToString();
            string markDown = dataGridViewEventArgs.Row.Cells["MarkDown"].Value.ToString();

            this.markdownEditorControl.Text = command;
            this.markdownEditorControl.FileContent = markDown;
        }


    }

    /// <summary>
    /// 命令实体
    /// </summary>
    class LinuxCommand
    {
        /// <summary>
        /// 命令
        /// </summary>
        public string n { get; set; }

        public string p { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string d { get; set; }
    }
}
